package customer.zspt.cn.customer.components.servex;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.util.List;

import customer.zspt.cn.customer.Adapter.ServexAdapter;
import customer.zspt.cn.customer.R;
import customer.zspt.cn.customer.bean.ServexDao;
import customer.zspt.cn.customer.constant.constant;
import customer.zspt.cn.customer.utils.SPUtils;

/**
 * Created by Administrator on 2018/6/16 0016.服务号
 */

public class servex extends Activity {
    private List<ServexDao.Info> infos;
    @ViewInject(R.id.servex_ls)
    private ListView servex_ls;
    @ViewInject(R.id.title)
    private View title;
    @ViewInject(R.id.UserName)
    private TextView UserName;
    @ViewInject(R.id.back)
    private ImageView back;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.servex);
        ViewUtils.inject(this);
        UserName.setText("我的服务号");
        View view = findViewById(R.id.empty_layout);
        servex_ls.setEmptyView(view);
        String servexData = SPUtils.getString(this, constant.SERVEX,"");
        if(!servexData.equals(""))
        {
            infos = setData(servexData);
            ServexAdapter myAdapter = new ServexAdapter(this,infos);
            servex_ls.setAdapter(myAdapter);
        }
        servex_ls.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String servexId = infos.get(position).toString();
                Intent intent = new Intent(getApplication(),servex_view.class);
                intent.putExtra("servexId",servexId);
                startActivity(intent);
            }
        });
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 准备服务号的数据
     */
    private List<ServexDao.Info> setData(String data) {
        Gson gson = new Gson();
        try{
            ServexDao servexdao = gson.fromJson(data, ServexDao.class);
            return servexdao.data;
        }catch (Exception e){
            return  null;
        }
    }
}
